package com.le.tester.javaThink.fifth.chapter24_ConcurrentProgramming;


import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * createTime：2021/8/18 11:46
 * description：QuittingCompletable
 */
public class QuittingCompletable {
    public static void main(String[] args) throws InterruptedException {
        List<QuittableTask> tasks = IntStream.range(1, QuittingTasks.COUNT)
                .mapToObj(QuittableTask::new)
                .collect(Collectors.toList());

        //把任务交给了CompletableFuture，用runAsync来执行任务
        List<CompletableFuture<Void>> cf = tasks.stream().map(CompletableFuture::runAsync)
                .collect(Collectors.toList());

        Thread.sleep(1000);
        tasks.forEach(QuittableTask::quit);
        cf.forEach(CompletableFuture::join);
    }
}
